package cn.frontop.funswitch.controller;

import cn.frontop.funswitch.common.CommonConstant;
import cn.frontop.funswitch.common.Result;
import cn.frontop.funswitch.entity.StaticDataEntity;
import cn.frontop.funswitch.param.StaticDataParam;
import cn.frontop.funswitch.service.StaticDataService;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 静态数据
 *
 * @author Yun
 * @email Yun@frontop.cn
 * @date 2022-10-21 11:41:24
 */
@RestController
@RequestMapping("/staticData")
@Api(tags = "静态xlsx文件管理")
public class StaticDataController {
    @Autowired
    private StaticDataService staticDataService;


    /**
     * 查看静态数据列表
     *
     * @param projectId 项目Id
     * @return 结果集
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation("查看静态数据列表")
    public Result<?> queryApiPathList(@RequestParam(name = "projectId") Integer projectId) {
        return Result.ok(staticDataService.getBaseMapper().selectList(new QueryWrapper<StaticDataEntity>().lambda().eq(StaticDataEntity::getProjectId, projectId)).stream().collect(Collectors.groupingBy(StaticDataEntity::getSeat)));
    }

    @PostMapping("/queryData")
    @ApiOperation("查询数据")
    public Result<List<StaticDataEntity>> queryData(@RequestBody StaticDataParam staticDataParam) {
        return Result.ok(staticDataService.getBaseMapper().selectList(new QueryWrapper<StaticDataEntity>().lambda().eq(StaticDataEntity::getProjectId, staticDataParam.getProjectId()).eq(StaticDataEntity::getSeat, staticDataParam.getName())));
    }


    @ApiOperation("上传文件")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "__File", paramType = "query")
    @PostMapping(path = "/warehousing", consumes = "multipart/form-data")
    public Result<String> warehousing(MultipartFile file, Integer projectId, String... sheets) {
        return staticDataService.warehousing(file, projectId, sheets);
    }

    /**
     * 下载模板
     */
    @PostMapping(value = "/downloadTemplate", produces = {"application/json;charset=UTF-8"})
    @ApiOperation("下载模板")
    public void downloadTemplate(HttpServletResponse response) throws UnsupportedEncodingException {
        ClassPathResource resource = new ClassPathResource(File.separator + "doc" + File.separator + "template.xlsx");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException ignored) {
        }
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=template.xlsx");
        resource.writeTo(out);
        IoUtil.close(out);
    }

    /**
     * 下载模板
     */
    @PostMapping(value = "/export", produces = {"application/json;charset=UTF-8"})
    @ApiOperation("导出")
    public void export(HttpServletResponse response) {
        ExcelWriter writer = ExcelUtil.getWriter(new File(CommonConstant.TEMPORARY_DIRECTORY + CommonConstant.FILE_NAME));
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException ignored) {
        }
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=template.xlsx");
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }
}
